package com.example.nyf.service;

import com.example.nyf.common.ConvertUtils;
import com.example.nyf.dto.SysUserTDO;
import com.example.nyf.entity.SysUser;
import com.example.nyf.entity.SysUserRole;
import com.example.nyf.mapper.SysRoleMenuMapper;
import com.example.nyf.mapper.SysUserMapper;
import com.example.nyf.mapper.SysUserRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Transactional
    public void user_save(SysUserTDO sysUserTDO) {
        if(!sysUserTDO.getPassword().isEmpty() && sysUserTDO.getPassword()!=null && !"".equals(sysUserTDO.getPassword())) {
            String passWord = sysUserTDO.getPassword();
            Md5Hash hash= new Md5Hash(passWord, "", 1024);
            sysUserTDO.setPassword(hash.toString());
        }

        SysUserRole sysUserRole = new SysUserRole();
        // 添加
        if(sysUserTDO.getId()==null || "".equals(sysUserTDO.getId())) {
            SysUser sysUser = sysUserMapper.getSysUserByName(sysUserTDO.getUser_name());
            if(!sysUser.getUser_name().isEmpty()) {
                throw new RuntimeException("用户名已存在");
            }

            sysUserMapper.user_add(ConvertUtils.sourceToTarget(sysUserTDO, SysUser.class));

            sysUserRole.setUser_id(sysUserTDO.getId());
            sysUserRole.setRole_id(sysUserTDO.getRole_id());
            sysUserRoleMapper.insertSysUserRole(sysUserRole);
        } else {
            // 修改
            // 更新用户表
            sysUserMapper.updateSysUser(sysUserTDO);
            // 更新用户角色表
            sysUserRole.setUser_id(sysUserTDO.getId());
            sysUserRole.setRole_id(sysUserTDO.getRole_id());
            sysUserRoleMapper.updateSysUserRole(sysUserRole);
        }

    }

    public SysUserTDO getSysUserById(String id) {
        return sysUserMapper.getSysUserById(id);
    }

    public SysUser getSysUserByName(String user_name) {
        return sysUserMapper.getSysUserByName(user_name);
    }

    /**
     * 返回用户数据 分页
     * @param pageNum 页数
     * @param pageSize 每页数量
     * @return
     */
    public HashMap getSysUserList(int pageNum, int pageSize, Map mp) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> sysUsers= sysUserMapper.getSysUserList(mp);
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);
        System.out.println(pageInfo.toString());
        HashMap list = new HashMap();
        list.put("total", pageInfo.getTotal());
        list.put("data", pageInfo.getList());
        //list.put("per_page", pageInfo.getSize());
        return list;
    }

    @Transactional
    public void deleteSysUser(Integer user_id) {
        sysUserMapper.deleteSysUser(user_id);
        sysUserRoleMapper.deleteSysUserRoleByUserId(user_id);
    }

    public int updateSysUserLoginInfo(SysUser sysUser) {
        return sysUserMapper.updateSysUserLoginInfo(sysUser);
    }
}
